<?php
require("includes/init.php");
require(ROOT_PATH . "includes/erp_report/erp_report_function.php");
$redis = getDeRedis();

// header("Access-Control-Allow-Origin: *");
// header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
// header("Access-Control-Allow-Headers: Content-Type");
//注意 关掉预检请求！！！
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    return;
}


// $_REQUEST['is_export'] = true;
if (isset($_REQUEST['GongShi']) && !empty($_REQUEST['GongShi'])) {
    $req = json_decode($_REQUEST['GongShi'],true);
    $req['date'] = date('Y-m-d',strtotime($req['date']));
    // var_dump($_REQUEST['GongShi']);
    $gongshi_list = $redis->get('GongShi');
    $gongshis = json_decode($gongshi_list,true);
    $gongshis = array_merge([$req],$gongshis);

    $redis->set('GongShi',json_encode($gongshis));
    echo json_encode(['code'=>0,'data'=>'']);
    return;
}if (isset($_REQUEST['is_export']) && $_REQUEST['is_export']) {
    $gongshi_list = $redis->get('GongShi');
    $res = getNormalAttachmentOut([array_merge(['技术开发工时'],json_decode($gongshi_list,true))],'DE电商-技术开发工时');
}else{
    $gongshi_list = $redis->get('GongShi');
    echo json_encode(['code'=>0,'data'=>['GongShi'=>json_decode($gongshi_list,true)]]);
    return;
}


function getDeRedis() {
    global $redis_config;
    require_once 'includes/predis-1.1/autoload.php';
    $redis = new Predis\Client([
          'host' => $redis_config['host'],
          'port' => $redis_config['port']
    ]);
    if ($redis_config['auth']) {
        $redis->auth($redis_config['auth']);
    }
    if ($redis_config['database']) {
        $redis->select($redis_config['database']);
    }
    return $redis;
}


